﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BizTalkConsoleObjects;
using BizTalkConsoleOperations;

namespace BizTalk_2010_Web_Console
{
    public partial class _Default : System.Web.UI.Page
    {
        BizTalkWebConsoleOM queriesManager = new BizTalkWebConsoleOM();
        static string groupNameAndMgmtDbStr;

        protected void Page_Load(object sender, EventArgs e)
        {
            BizTalkConsoleObjects.BizTalkWebConsoleActions.SetConnections(System.Configuration.ConfigurationManager.AppSettings["BizTalkMgmtDBName"], System.Configuration.ConfigurationManager.AppSettings["BizTalkMgmtDBServer"]);
            BizTalkConsoleObjects.BizTalkWebConsoleOM.SetConnections(System.Configuration.ConfigurationManager.AppSettings["BizTalkMgmtDBName"], System.Configuration.ConfigurationManager.AppSettings["BizTalkMgmtDBServer"]);

            if (!IsPostBack)
            {
                PopulateQueryDropdowns();
            }

            if (BizTalkServerAdministrationTreeView.Nodes.Count == 0)
            {

                TreeNode RootNode = new TreeNode("BizTalk Server Administration");

                GroupNameAndMgmtDb groupNameAndMgmtDb = BizTalkConsoleObjects.BizTalkWebConsoleOM.GetServerAndManagementDbName();
                groupNameAndMgmtDbStr = "BizTalk Group [" + groupNameAndMgmtDb.MachineName + ":" + groupNameAndMgmtDb.MgmtDb + "]";
                TreeNode BizTalkGroupNode = new TreeNode(groupNameAndMgmtDbStr);

                TreeNode ApplicationsNode = new TreeNode("Applications");

                TreeNode PlatformSettingsNode = new TreeNode("Platform Settings");
                TreeNode HostsNode = new TreeNode("Hosts");

                TreeNode HostInstancesNode = new TreeNode("Host Instances");
                TreeNode ServersNode = new TreeNode("Servers");
                TreeNode MessageBoxesNode = new TreeNode("Message Boxes");
                TreeNode AdaptersNode = new TreeNode("Adapters");

                PlatformSettingsNode.ChildNodes.Add(HostsNode);
                PlatformSettingsNode.ChildNodes.Add(HostInstancesNode);
                PlatformSettingsNode.ChildNodes.Add(ServersNode);
                PlatformSettingsNode.ChildNodes.Add(MessageBoxesNode);
                PlatformSettingsNode.ChildNodes.Add(AdaptersNode);

                BizTalkGroupNode.ChildNodes.Add(ApplicationsNode);
                BizTalkGroupNode.ChildNodes.Add(PlatformSettingsNode);
                RootNode.ChildNodes.Add(BizTalkGroupNode);


                BizTalkServerAdministrationTreeView.Nodes.Add(RootNode);
            }
        }

        protected void BizTalkServerAdministrationTreeView_SelectedNodeChanged(object sender, EventArgs e)
        {
            string SelectedNode = BizTalkServerAdministrationTreeView.SelectedValue.ToString();
            string ParentNode = "";

            BizTalkConsoleGridView.Controls.Clear();
            PanelBizTalkQueries.Visible = false;
            ActionsDataList.Visible = false;

            if (BizTalkServerAdministrationTreeView.SelectedNode.Parent != null)
            {
                ParentNode = BizTalkServerAdministrationTreeView.SelectedNode.Parent.Text;

                if (ParentNode == "Applications")
                {
                    BizTalkConsoleGridView.AutoGenerateSelectButton = false;
                    BizTalkServerAdministrationTreeView.SelectedNode.ChildNodes.Clear();
                    TreeNode applicationTree = GetApplicationTree(BizTalkServerAdministrationTreeView.SelectedNode);
                }
                else if (SelectedNode == "Applications")
                {
                    BizTalkConsoleGridView.AutoGenerateSelectButton = false;
                    if (BizTalkServerAdministrationTreeView.SelectedNode.ChildNodes.Count == 0)
                    {
                        PopulateApplicationTree();
                    }
                }
                else if (SelectedNode.StartsWith("BizTalk Group") && ParentNode == "BizTalk Server Administration")
                {
                    BizTalkConsoleGridView.AutoGenerateSelectButton = false;
                    PanelBizTalkQueries.Visible = true;
                }

                #region Application Artifacts

                if (BizTalkServerAdministrationTreeView.SelectedNode.Parent.Parent != null)
                {
                    if (BizTalkServerAdministrationTreeView.SelectedNode.Parent.Parent.Text == "Applications")
                    {
                        string SelectedArtifact = BizTalkServerAdministrationTreeView.SelectedNode.Text;
                        string ApplicationName = BizTalkServerAdministrationTreeView.SelectedNode.Parent.Text;

                        ActionsDataList.Visible = false;

                        if (SelectedArtifact == "Orchestrations")
                        {
                            BizTalkConsoleGridView.AutoGenerateSelectButton = true;
                            GetOrchestrations(ApplicationName);
                        }
                        else if (SelectedArtifact == "Role Links")
                        {
                            BizTalkConsoleGridView.AutoGenerateSelectButton = false;
                            List<BizTalkConsoleObjects.RoleLinks> RoleLinks = BizTalkConsoleObjects.BizTalkWebConsoleOM.GetRoleLinks(ApplicationName);
                            BizTalkConsoleGridView.DataSource = RoleLinks;
                            BizTalkConsoleGridView.DataBind();
                        }
                        else if (SelectedArtifact == "Send Port Groups")
                        {
                            BizTalkConsoleGridView.AutoGenerateSelectButton = true;
                            GetSendPortGroups(ApplicationName);
                        }
                        else if (SelectedArtifact == "Send Ports")
                        {
                            BizTalkConsoleGridView.AutoGenerateSelectButton = true;
                            GetSendPorts(ApplicationName);
                        }
                        else if (SelectedArtifact == "Receive Ports")
                        {
                            BizTalkConsoleGridView.AutoGenerateSelectButton = false;
                            List<BizTalkConsoleObjects.ReceivePorts> ReceivePorts = BizTalkConsoleObjects.BizTalkWebConsoleOM.GetReceivePorts(ApplicationName);
                            BizTalkConsoleGridView.DataSource = ReceivePorts;
                            BizTalkConsoleGridView.DataBind();
                        }
                        else if (SelectedArtifact == "Receive Locations")
                        {
                            BizTalkConsoleGridView.AutoGenerateSelectButton = true;
                            GetReceiveLocations(ApplicationName);
                        }
                        else if (SelectedArtifact == "Policies")
                        {
                            BizTalkConsoleGridView.AutoGenerateSelectButton = false;
                            List<BizTalkConsoleObjects.Policies> Policies = BizTalkConsoleObjects.BizTalkWebConsoleOM.GetPolicies(ApplicationName);
                            BizTalkConsoleGridView.DataSource = Policies;
                            BizTalkConsoleGridView.DataBind();
                        }
                        else if (SelectedArtifact == "Schemas")
                        {
                            BizTalkConsoleGridView.AutoGenerateSelectButton = false;
                            List<BizTalkConsoleObjects.Schemas> Schemas = BizTalkConsoleObjects.BizTalkWebConsoleOM.GetSchemas(ApplicationName);
                            BizTalkConsoleGridView.DataSource = Schemas;
                            BizTalkConsoleGridView.DataBind();
                        }
                        else if (SelectedArtifact == "Maps")
                        {
                            BizTalkConsoleGridView.AutoGenerateSelectButton = false;
                            List<BizTalkConsoleObjects.Maps> Maps = BizTalkConsoleObjects.BizTalkWebConsoleOM.GetMaps(ApplicationName);
                            BizTalkConsoleGridView.DataSource = Maps;
                            BizTalkConsoleGridView.DataBind();
                        }
                        else if (SelectedArtifact == "Pipelines")
                        {
                            BizTalkConsoleGridView.AutoGenerateSelectButton = false;
                            List<BizTalkConsoleObjects.Pipelines> Pipelines = BizTalkConsoleObjects.BizTalkWebConsoleOM.GetPipelines(ApplicationName);
                            BizTalkConsoleGridView.DataSource = Pipelines;
                            BizTalkConsoleGridView.DataBind();
                        }
                        else if (SelectedArtifact == "Resources")
                        {
                            BizTalkConsoleGridView.AutoGenerateSelectButton = false;
                            List<BizTalkConsoleObjects.Resources> Resources = BizTalkConsoleObjects.BizTalkWebConsoleOM.GetResources(ApplicationName);
                            BizTalkConsoleGridView.DataSource = Resources;
                            BizTalkConsoleGridView.DataBind();
                        }
                    }
                }

                #endregion

                #region Platform Settings

                if (ParentNode == "Platform Settings")
                {
                    if (SelectedNode == "Hosts")
                    {
                        BizTalkConsoleGridView.AutoGenerateSelectButton = false;
                        List<BizTalkConsoleObjects.Hosts> Hosts = BizTalkConsoleObjects.BizTalkWebConsoleOM.GetHosts();

                        BizTalkConsoleGridView.DataSource = Hosts;
                        BizTalkConsoleGridView.DataBind();

                    }
                    else if (SelectedNode == "Host Instances")
                    {
                        BizTalkConsoleGridView.AutoGenerateSelectButton = true;
                        RefreshHostInstances();
                    }
                    else if (SelectedNode == "Message Boxes")
                    {
                        BizTalkConsoleGridView.AutoGenerateSelectButton = false;
                        List<MessageBoxes> msgBoxSettings = BizTalkConsoleObjects.BizTalkWebConsoleOM.GetMessageBoxSetting();
                        BizTalkConsoleGridView.DataSource = msgBoxSettings;
                        BizTalkConsoleGridView.DataBind();
                    }
                    else if (SelectedNode == "Servers")
                    {
                        BizTalkConsoleGridView.AutoGenerateSelectButton = false;
                        List<Servers> server = BizTalkConsoleObjects.BizTalkWebConsoleOM.GetServerName();
                        BizTalkConsoleGridView.DataSource = server;
                        BizTalkConsoleGridView.DataBind();
                    }
                    else if (SelectedNode == "Adapters")
                    {
                        BizTalkConsoleGridView.AutoGenerateSelectButton = false;
                        BizTalkServerAdministrationTreeView.SelectedNode.ChildNodes.Clear();
                        PopulateAdaptersTree(BizTalkServerAdministrationTreeView.SelectedNode);
                    }
                }

                #endregion

            }
        }

        private void PopulateApplicationTree()
        {
            BizTalkServerAdministrationTreeView.FindNode("BizTalk Server Administration/" + groupNameAndMgmtDbStr + "/Applications").ChildNodes.Clear();
            List<BizTalkConsoleObjects.Applications> applications = BizTalkConsoleObjects.BizTalkWebConsoleOM.GetApplications();
            foreach (BizTalkConsoleObjects.Applications application in applications)
            {
                BizTalkServerAdministrationTreeView.SelectedNode.ChildNodes.Add(new TreeNode(application.Name));
                TreeNode applicationTree = GetApplicationTree(BizTalkServerAdministrationTreeView.SelectedNode.ChildNodes[BizTalkServerAdministrationTreeView.SelectedNode.ChildNodes.Count - 1]);
            }
        }

        private TreeNode GetApplicationTree(TreeNode applicationTree)
        {
            applicationTree.ChildNodes.Add(new TreeNode("Orchestrations"));
            applicationTree.ChildNodes.Add(new TreeNode("Role Links"));
            applicationTree.ChildNodes.Add(new TreeNode("Send Port Groups"));
            applicationTree.ChildNodes.Add(new TreeNode("Send Ports"));
            applicationTree.ChildNodes.Add(new TreeNode("Receive Ports"));
            applicationTree.ChildNodes.Add(new TreeNode("Receive Locations"));
            applicationTree.ChildNodes.Add(new TreeNode("Policies"));
            applicationTree.ChildNodes.Add(new TreeNode("Schemas"));
            applicationTree.ChildNodes.Add(new TreeNode("Maps"));
            applicationTree.ChildNodes.Add(new TreeNode("Pipelines"));
            applicationTree.ChildNodes.Add(new TreeNode("Resources"));
            return applicationTree;
        }

        protected void BizTalkConsoleGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (BizTalkServerAdministrationTreeView.SelectedNode.Parent.Parent != null)
            {
                if (BizTalkServerAdministrationTreeView.SelectedNode.Parent.Parent.Text == "Applications")
                {
                    string SelectedApplication = BizTalkServerAdministrationTreeView.SelectedNode.Parent.Text;
                    string SelectedArtifact = BizTalkServerAdministrationTreeView.SelectedNode.Text;

                    GridViewRow artifactRow = BizTalkConsoleGridView.SelectedRow;

                    if (SelectedArtifact == "Orchestrations")
                    {
                        ActionsDataList.DataSource = BizTalkWebConsoleActions.OrchestrationActions.GetOrchestrationAction(BizTalkConsoleGridView.SelectedRow.Cells[1].Text, SelectedApplication);
                        ActionsDataList.DataBind();
                        ActionsDataList.Visible = true;
                    }
                    else if (SelectedArtifact == "Receive Locations")
                    {
                        ActionsDataList.DataSource = BizTalkWebConsoleActions.ReceiveLocationActions.GetReceiveLocationAction(BizTalkConsoleGridView.SelectedRow.Cells[1].Text, BizTalkConsoleGridView.SelectedRow.Cells[5].Text, SelectedApplication);
                        ActionsDataList.DataBind();
                        ActionsDataList.Visible = true;
                    }
                    else if (SelectedArtifact == "Send Ports")
                    {
                        ActionsDataList.DataSource = BizTalkWebConsoleActions.SendPortActions.GetSendPortAction(BizTalkConsoleGridView.SelectedRow.Cells[1].Text, SelectedApplication);
                        ActionsDataList.DataBind();
                        ActionsDataList.Visible = true;
                    }
                    else if (SelectedArtifact == "Send Port Groups")
                    {
                        ActionsDataList.DataSource = BizTalkWebConsoleActions.SendPortGroupsActions.GetSendPortGroupsAction(BizTalkConsoleGridView.SelectedRow.Cells[1].Text, SelectedApplication);
                        ActionsDataList.DataBind();
                        ActionsDataList.Visible = true;
                    }
                }
                else if (BizTalkServerAdministrationTreeView.SelectedNode.Parent.Text == "Platform Settings")
                {
                    if (BizTalkServerAdministrationTreeView.SelectedNode.Text == "Host Instances")
                    {
                        ActionsDataList.DataSource = BizTalkWebConsoleActions.HostInstancesActions.GetHostInstanceAction(BizTalkConsoleGridView.SelectedRow.Cells[1].Text);
                        ActionsDataList.DataBind();
                        ActionsDataList.Visible = true;
                    }
                }
            }
        }

        protected void LinkButtonAction_Click(object sender, EventArgs e)
        {
            LinkButton linkbutton = (LinkButton)sender;

            if (BizTalkServerAdministrationTreeView.SelectedNode.Parent.Parent.Text == "Applications")
            {
                string SelectedApplication = BizTalkServerAdministrationTreeView.SelectedNode.Parent.Text;
                string SelectedArtifact = BizTalkServerAdministrationTreeView.SelectedNode.Text;

                if (SelectedArtifact == "Orchestrations")
                {
                    switch (linkbutton.Text)
                    {
                        case "Start":
                            BizTalkWebConsoleActions.OrchestrationActions.SetOrchestrationStatus(BizTalkConsoleGridView.SelectedRow.Cells[1].Text, SelectedApplication, Microsoft.BizTalk.ExplorerOM.OrchestrationStatus.Started);
                            break;
                        case "Unenlist":
                            BizTalkWebConsoleActions.OrchestrationActions.SetOrchestrationStatus(BizTalkConsoleGridView.SelectedRow.Cells[1].Text, SelectedApplication, Microsoft.BizTalk.ExplorerOM.OrchestrationStatus.Unenlisted);
                            break;
                        case "Stop":
                            BizTalkWebConsoleActions.OrchestrationActions.SetOrchestrationStatus(BizTalkConsoleGridView.SelectedRow.Cells[1].Text, SelectedApplication, Microsoft.BizTalk.ExplorerOM.OrchestrationStatus.Enlisted);
                            break;
                        case "Enlist":
                            BizTalkWebConsoleActions.OrchestrationActions.SetOrchestrationStatus(BizTalkConsoleGridView.SelectedRow.Cells[1].Text, SelectedApplication, Microsoft.BizTalk.ExplorerOM.OrchestrationStatus.Enlisted);
                            break;
                    }

                    GetOrchestrations(SelectedApplication);
                }
                else if (SelectedArtifact == "Receive Locations")
                {
                    switch (linkbutton.Text)
                    {
                        case "Enable":
                            BizTalkWebConsoleActions.ReceiveLocationActions.SetReceiveLocationStatus(BizTalkConsoleGridView.SelectedRow.Cells[1].Text, BizTalkConsoleGridView.SelectedRow.Cells[5].Text, SelectedApplication, true);
                            break;

                        case "Disable":
                            BizTalkWebConsoleActions.ReceiveLocationActions.SetReceiveLocationStatus(BizTalkConsoleGridView.SelectedRow.Cells[1].Text, BizTalkConsoleGridView.SelectedRow.Cells[5].Text, SelectedApplication, false);
                            break;
                    }

                    GetReceiveLocations(SelectedApplication);
                }
                else if (SelectedArtifact == "Send Ports")
                {
                    switch (linkbutton.Text)
                    {
                        case "Start":
                            BizTalkWebConsoleActions.SendPortActions.SetSendPortStatus(BizTalkConsoleGridView.SelectedRow.Cells[1].Text, SelectedApplication, Microsoft.BizTalk.ExplorerOM.PortStatus.Started);
                            break;
                        case "Unenlist":
                            BizTalkWebConsoleActions.SendPortActions.SetSendPortStatus(BizTalkConsoleGridView.SelectedRow.Cells[1].Text, SelectedApplication, Microsoft.BizTalk.ExplorerOM.PortStatus.Bound);
                            break;
                        case "Stop":
                            BizTalkWebConsoleActions.SendPortActions.SetSendPortStatus(BizTalkConsoleGridView.SelectedRow.Cells[1].Text, SelectedApplication, Microsoft.BizTalk.ExplorerOM.PortStatus.Stopped);
                            break;
                        case "Enlist":
                            BizTalkWebConsoleActions.SendPortActions.SetSendPortStatus(BizTalkConsoleGridView.SelectedRow.Cells[1].Text, SelectedApplication, Microsoft.BizTalk.ExplorerOM.PortStatus.Stopped);
                            break;
                    }

                    GetSendPorts(SelectedApplication);
                }
                else if (SelectedArtifact == "Send Port Groups")
                {
                    switch (linkbutton.Text)
                    {
                        case "Start":
                            BizTalkWebConsoleActions.SendPortGroupsActions.SetSendPortGroupsStatus(BizTalkConsoleGridView.SelectedRow.Cells[1].Text, SelectedApplication, Microsoft.BizTalk.ExplorerOM.PortStatus.Started);
                            break;
                        case "Unenlist":
                            BizTalkWebConsoleActions.SendPortGroupsActions.SetSendPortGroupsStatus(BizTalkConsoleGridView.SelectedRow.Cells[1].Text, SelectedApplication, Microsoft.BizTalk.ExplorerOM.PortStatus.Bound);
                            break;
                        case "Stop":
                            BizTalkWebConsoleActions.SendPortGroupsActions.SetSendPortGroupsStatus(BizTalkConsoleGridView.SelectedRow.Cells[1].Text, SelectedApplication, Microsoft.BizTalk.ExplorerOM.PortStatus.Stopped);
                            break;
                        case "Enlist":
                            BizTalkWebConsoleActions.SendPortGroupsActions.SetSendPortGroupsStatus(BizTalkConsoleGridView.SelectedRow.Cells[1].Text, SelectedApplication, Microsoft.BizTalk.ExplorerOM.PortStatus.Stopped);
                            break;
                    }

                    GetSendPortGroups(SelectedApplication);
                }
            }
            if (BizTalkServerAdministrationTreeView.SelectedNode.Parent.Text == "Platform Settings")
            {
                if (BizTalkServerAdministrationTreeView.SelectedNode.Text == "Host Instances")
                {
                    switch (linkbutton.Text)
                    {
                        case "Start":
                            BizTalkWebConsoleActions.HostInstancesActions.StartHostInstance(BizTalkConsoleGridView.SelectedRow.Cells[1].Text);
                            break;

                        case "Stop":
                            BizTalkWebConsoleActions.HostInstancesActions.StopHostInstance(BizTalkConsoleGridView.SelectedRow.Cells[1].Text);
                            break;

                        case "Restart":
                            BizTalkWebConsoleActions.HostInstancesActions.RestartHostInstance(BizTalkConsoleGridView.SelectedRow.Cells[1].Text);
                            break;
                    }
                    RefreshHostInstances();
                }
            }

            ActionsDataList.Visible = false;
        }

        private void RefreshHostInstances()
        {
            List<BizTalkConsoleObjects.HostInstances> hostInstances = BizTalkConsoleObjects.BizTalkWebConsoleOM.GetHostInstances();
            BizTalkConsoleGridView.DataSource = hostInstances;
            BizTalkConsoleGridView.DataBind();
        }

        private void GetOrchestrations(string ApplicationName)
        {
            List<BizTalkConsoleObjects.Orchestrations> Orchestrations = BizTalkConsoleObjects.BizTalkWebConsoleOM.GetOrchestrations(ApplicationName);
            BizTalkConsoleGridView.DataSource = Orchestrations;
            BizTalkConsoleGridView.DataBind();
        }

        private void GetReceiveLocations(string ApplicationName)
        {
            List<BizTalkConsoleObjects.ReceiveLocations> ReceiveLocations = BizTalkConsoleObjects.BizTalkWebConsoleOM.GetReceiveLocations(ApplicationName);
            BizTalkConsoleGridView.DataSource = ReceiveLocations;
            BizTalkConsoleGridView.DataBind();
        }

        private void GetSendPorts(string ApplicationName)
        {
            List<BizTalkConsoleObjects.SendPorts> SendPorts = BizTalkConsoleObjects.BizTalkWebConsoleOM.GetSendPorts(ApplicationName);
            BizTalkConsoleGridView.DataSource = SendPorts;
            BizTalkConsoleGridView.DataBind();
        }

        private void GetSendPortGroups(string ApplicationName)
        {
            List<BizTalkConsoleObjects.SendPortGroups> SendPortGroups = BizTalkConsoleObjects.BizTalkWebConsoleOM.GetSendPortGroups(ApplicationName);
            BizTalkConsoleGridView.DataSource = SendPortGroups;
            BizTalkConsoleGridView.DataBind();
        }

        private void PopulateAdaptersTree(TreeNode adaptersTreeNode)
        {
            List<BizTalkConsoleObjects.Adapters> adapters = BizTalkConsoleObjects.BizTalkWebConsoleOM.GetAdapters();
            foreach (BizTalkConsoleObjects.Adapters adapter in adapters)
            {
                adaptersTreeNode.ChildNodes.Add(new TreeNode(adapter.Name));
            }
        }

        private void PopulateQueryDropdowns()
        {
            List<BizTalkConsoleObjects.Applications> applications = BizTalkConsoleObjects.BizTalkWebConsoleOM.GetApplications();

            DropDownListApplicationName.DataTextField = "Name";
            DropDownListApplicationName.DataValueField = "Name";
            DropDownListApplicationName.DataSource = applications;
            DropDownListApplicationName.DataBind();

            List<BizTalkConsoleObjects.HostInstances> hostInstances = BizTalkConsoleObjects.BizTalkWebConsoleOM.GetHostInstances();
            DropDownListHostName.DataTextField = "HostName";
            DropDownListHostName.DataValueField = "HostName";
            DropDownListHostName.DataSource = hostInstances;
            DropDownListHostName.DataBind();
        }

        protected void DropDownListSearchFor_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownListInstanceStatus.Items.Clear();

            switch (DropDownListSearchFor.SelectedValue)
            {

                case "All":
                    DropDownListInstanceStatus.Items.Add(new ListItem("None", "None"));
                    DropDownListInstanceStatus.Items.Add(new ListItem("All Running", "RunningAll"));
                    DropDownListInstanceStatus.Items.Add(new ListItem("All Suspended", "SuspendedAll"));
                    DropDownListInstanceStatus.Items.Add(new ListItem("Active", "Active"));
                    DropDownListInstanceStatus.Items.Add(new ListItem("Dehydrated", "Dehydrated"));
                    DropDownListInstanceStatus.Items.Add(new ListItem("Ready to run", "ReadyToRun"));
                    DropDownListInstanceStatus.Items.Add(new ListItem("Scheduled", "Scheduled"));
                    DropDownListInstanceStatus.Items.Add(new ListItem("Suspended (not resumable)", "SuspendedNotResumable"));
                    DropDownListInstanceStatus.Items.Add(new ListItem("Suspended (resumable)", "Suspended"));
                    break;

                case "RunningAll":
                    DropDownListInstanceStatus.Items.Add(new ListItem("None", "None"));
                    DropDownListInstanceStatus.Items.Add(new ListItem("Active", "Active"));
                    DropDownListInstanceStatus.Items.Add(new ListItem("Dehydrated", "Dehydrated"));
                    DropDownListInstanceStatus.Items.Add(new ListItem("Ready to run", "ReadyToRun"));
                    DropDownListInstanceStatus.Items.Add(new ListItem("Scheduled", "Scheduled"));
                    break;

                case "SuspendedAll":
                    DropDownListInstanceStatus.Items.Add(new ListItem("None", "None"));
                    DropDownListInstanceStatus.Items.Add(new ListItem("Suspended (not resumable)", "SuspendedNotResumable"));
                    DropDownListInstanceStatus.Items.Add(new ListItem("Suspended (resumable)", "Suspended"));
                    break;
            }
        }

        protected void ButtonRunQuery_Click(object sender, EventArgs e)
        {
            Microsoft.BizTalk.Operations.InstanceStatus instanceStatus;

            if (DropDownListInstanceStatus.SelectedValue == "None")
            {
                Enum.TryParse<Microsoft.BizTalk.Operations.InstanceStatus>(DropDownListSearchFor.SelectedValue, out instanceStatus);

            }
            else
            {
                Enum.TryParse<Microsoft.BizTalk.Operations.InstanceStatus>(DropDownListInstanceStatus.SelectedValue, out instanceStatus);
            }

            Microsoft.BizTalk.Operations.ServiceClass serviceClass;
            Enum.TryParse<Microsoft.BizTalk.Operations.ServiceClass>(DropDownListServiceClass.SelectedValue, out serviceClass);

            BizTalkConsoleGridView.DataSource = BizTalkWebConsoleOperations.GetServiceInstances(instanceStatus, Convert.ToInt32(TextBoxMaximumMatches.Text), DropDownListApplicationName.SelectedItem.Text, DropDownListHostName.SelectedItem.Value, serviceClass);
            BizTalkConsoleGridView.DataBind();

            LabelQueryResult.Text = BizTalkConsoleGridView.Rows.Count.ToString();
        }
    }
}